<template>
  <div class="page-container login">
    <div class="header">
      <van-icon name="arrow-left" @click="router.back()" />
    </div>
    
    <div class="content">
      <div class="logo">
        <div class="icon-wrapper">
          <div class="icon-group">
            <van-icon name="friends-o" class="icon-main" />
            <van-icon name="plus" class="icon-badge" />
          </div>
          <div class="icon-circle"></div>
        </div>
        <div class="platform-name">陪诊服务平台</div>
      </div>

      <div class="welcome">
        <p>请登录您的账号</p>
      </div>

      <div class="form">
        <van-form @submit="onSubmit">
          <van-cell-group inset>
            <van-field
              v-model="formData.username"
              name="username"
              placeholder="请输入用户名"
              :rules="[{ required: true, message: '请输入用户名' }]"
            >
              <template #left-icon>
                <van-icon name="user-o" color="#666" size="20" />
              </template>
            </van-field>
            <van-field
              v-model="formData.password"
              type="password"
              name="password"
              placeholder="请输入密码"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <template #left-icon>
                <van-icon name="lock" color="#666" size="20" />
              </template>
            </van-field>
            <van-field
              v-model="formData.captcha"
              name="captcha"
              placeholder="请输入验证码"
              :rules="[{ required: true, message: '请输入验证码' }]"
            >
              <template #left-icon>
                <van-icon name="shield-o" color="#666" size="20" style="margin-top: 2.5vw;" />
              </template>
              <template #right-icon>
                <img 
                  v-if="captchaUrl" 
                  :src="captchaUrl" 
                  class="captcha-img" 
                  @click="refreshCaptcha"
                  alt="验证码"
                />
              </template>
            </van-field>
          </van-cell-group>

          <!-- <div class="form-footer">
            <span class="forgot" @click="onForgotPassword">忘记密码？</span>
          </div> -->

          <div class="submit-btn">
            <van-button round block type="primary" native-type="submit">
              登录
            </van-button>
          </div>
        </van-form>

        <div class="register">
          还没有账号？ <span @click="goToRegister">立即注册</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast, showLoadingToast } from 'vant'
import { login, captcha, current } from '@/api/user'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const formData = ref({
  username: '',
  password: '',
  captcha: ''
})

const captchaUrl = ref('')

// 获取验证码
const getCaptcha = async () => {
  try {
    const res = await captcha()
    if (res.code === 200 && res.data) {
      captchaUrl.value = res.data.image
    } else {
      showToast('获取验证码失败')
    }
  } catch (error) {
    showToast('获取验证码失败')
  }
}

// 刷新验证码
const refreshCaptcha = () => {
  formData.value.captcha = ''
  getCaptcha()
}

const onSubmit = async () => {
  try {
    const loading = showLoadingToast({
      message: '登录中...',
      forbidClick: true,
    })
    
    const res = await login({
      username: formData.value.username,
      password: formData.value.password,
      captcha: formData.value.captcha
    })
    
    loading.close()
    
    if (res.code === 200) {
      // 保存token
      userStore.setToken(res.data)
      
      // 获取并保存用户信息
      try {
        const userRes = await current()
        if (userRes.code === 200) {
          userStore.setUserInfo(userRes.data)
        }
      } catch (error) {
        console.error('获取用户信息失败:', error)
      }
      
      showToast({
        message: '登录成功',
        type: 'success'
      })
      
      // 获取重定向地址
      const redirect = router.currentRoute.value.query.redirect || '/home'
      router.replace(redirect)
    }
  } catch (error) {
    showToast({
      message: error.message || '登录失败',
      type: 'fail'
    })
    // 登录失败时刷新验证码
    refreshCaptcha()
  }
}

// 页面加载时获取验证码
onMounted(() => {
  getCaptcha()
})

const onForgotPassword = () => {
  // TODO: 实现忘记密码逻辑
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style lang="scss" scoped>
.login {
  min-height: 100vh;
  background: #fff;
  display: flex;
  flex-direction: column;

  .header {
    padding: 16px;
    
    .van-icon {
      font-size: 24px;
      color: #333;
      cursor: pointer;
      transition: opacity 0.2s;

      &:hover {
        opacity: 0.8;
      }
    }
  }

  .content {
    flex: 1;
    padding: 0 24px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    max-width: 400px;
    margin: 0 auto;
    width: 100%;

    .logo {
      margin-bottom: 40px;
      display: flex;
      flex-direction: column;
      align-items: center;
      
      .icon-wrapper {
        position: relative;
        width: 88px;
        height: 88px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 16px;
        
        .icon-group {
          position: relative;
          z-index: 1;

          .icon-main {
            font-size: 44px;
            color: var(--primary-color);
          }

          .icon-badge {
            position: absolute;
            right: -12px;
            bottom: -4px;
            font-size: 20px;
            color: #fff;
            background-color: var(--primary-color);
            border-radius: 50%;
            padding: 4px;
            box-shadow: 0 2px 8px rgba(11, 181, 132, 0.3);
          }
        }

        .icon-circle {
          position: absolute;
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background: linear-gradient(135deg, rgba(11, 181, 132, 0.1), rgba(20, 211, 139, 0.1));
          animation: pulse 2s infinite;
        }

        &::after {
          content: '';
          position: absolute;
          width: 70px;
          height: 70px;
          border-radius: 50%;
          background: linear-gradient(135deg, rgba(11, 181, 132, 0.2), rgba(20, 211, 139, 0.2));
        }
      }

      .platform-name {
        font-size: 20px;
        font-weight: 500;
        color: var(--primary-color);
        letter-spacing: 1px;
        background: linear-gradient(to right, var(--primary-color), #14d38b);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      }
    }

    .welcome {
      margin-bottom: 48px;
      text-align: center;
      
      h2 {
        font-size: 32px;
        font-weight: 600;
        color: var(--text-color);
        margin: 0 0 12px;
        letter-spacing: 1px;
      }

      p {
        font-size: 16px;
        color: var(--text-color-secondary);
        margin: 0;
        letter-spacing: 0.5px;
      }
    }

    .form {
      .van-cell-group {
        margin-bottom: 24px;
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
        
        :deep(.van-field) {
          padding: 16px;

          .van-field__left-icon {
            margin-right: 12px;
          }

          .van-field__control {
            font-size: 16px;
            color: var(--text-color);

            &::placeholder {
              color: #999;
            }
          }
        }

        .captcha-img {
          height: 36px;
          border-radius: 4px;
          cursor: pointer;
          transition: opacity 0.2s;

          &:hover {
            opacity: 0.8;
          }
        }
      }

      .form-footer {
        display: flex;
        justify-content: flex-end;
        margin-bottom: 32px;

        .forgot {
          color: var(--primary-color);
          font-size: 14px;
          cursor: pointer;
          transition: opacity 0.2s;

          &:hover {
            opacity: 0.8;
          }
        }
      }

      .submit-btn {
        margin-bottom: 32px;

        :deep(.van-button) {
          height: 48px;
          font-size: 16px;
          font-weight: 500;
          letter-spacing: 1px;
          background: linear-gradient(to right, var(--primary-color), #14d38b);
          border: none;
          transition: transform 0.2s;

          &:active {
            transform: scale(0.98);
          }
        }
      }

      .register {
        text-align: center;
        font-size: 14px;
        color: var(--text-color-secondary);

        span {
          color: var(--primary-color);
          margin-left: 4px;
          cursor: pointer;
          transition: opacity 0.2s;

          &:hover {
            opacity: 0.8;
          }
        }
      }
    }
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
</style> 